home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SplitPanePanel.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  317 lines

  1. /*
  2.  * @(#)SplitPanePanel.java    1.9 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.event.*;
  23. import com.sun.java.accessibility.*;
  24.  
  25. import java.awt.*;
  26. import java.awt.event.*;
  27.  
  28. /*
  29.  * An example of using a JSplitPane. The key thing to remember about using
  30.  * a JSplitPane is that it used the minimum size to determine where the
  31.  * components can be resize to! Some of the components in swing will return
  32.  * the current size as the minimum size, meaning JSplitPane will not allow
  33.  * you to resize it. Luckily JComponent has methods for setting the
  34.  * preferred/min sizes.
  35.  *
  36.  * @version 1.9 02/02/98
  37.  * @author Scott Violet
  38.  * @author Peter Korn (accessibility support)
  39.  */
  40. public class SplitPanePanel extends JPanel
  41. {
  42.     /** JSplitPane being shown to the user. */
  43.     protected JSplitPane             splitPane;
  44.     /** Left component being split. */
  45.     protected GridComponent          leftGrid;
  46.     /** Right component being split. */
  47.     protected GridComponent          rightGrid;
  48.     /** Grand puba swingset. */
  49.     protected SwingSet               swing;
  50.  
  51.     public SplitPanePanel(SwingSet swing) {
  52.     super();
  53.     this.swing = swing;
  54.     setDoubleBuffered(true);
  55.     setLayout(new BorderLayout());
  56.     createSplitPane();
  57.     createInformationControls();
  58.     }
  59.  
  60.     /**
  61.      * Creates the JSplitPane.
  62.      */
  63.     protected void createSplitPane() {
  64.     leftGrid = new GridComponent(4);
  65.     leftGrid.setPreferredSize(10);
  66.     rightGrid = new GridComponent(4);
  67.     rightGrid.setPreferredSize(10);
  68.     splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftGrid,
  69.                    rightGrid);
  70.     splitPane.setContinuousLayout(true);
  71.     splitPane.setPreferredSize(new Dimension(400, 100));
  72.     splitPane.getAccessibleContext().setAccessibleName("Split pane example");
  73.     add(splitPane, BorderLayout.CENTER);
  74.     }
  75.  
  76.     /**
  77.      * Creates controls to alter the JSplitPane.
  78.      */
  79.     protected void createInformationControls() {
  80.     JPanel                wrapper = new JPanel();
  81.     ButtonGroup           group = new ButtonGroup();
  82.     JRadioButton          button;
  83.  
  84.     Box                   buttonWrapper = new Box(BoxLayout.X_AXIS);
  85.  
  86.     wrapper.setLayout(new GridLayout(0, 1));
  87.  
  88.     /* Create a radio button to vertically split the split pane. */
  89.     button = new JRadioButton("Vertically split");
  90.     button.setMnemonic('V');
  91.     button.addActionListener(new ActionListener() {
  92.         public void actionPerformed(ActionEvent e) {
  93.         splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  94.         }
  95.     });
  96.     group.add(button);
  97.     buttonWrapper.add(button);
  98.  
  99.     /* Create a radio button the horizontally split the split pane. */
  100.     button = new JRadioButton("Horizontally split");
  101.     button.setMnemonic('r');
  102.     button.setSelected(true);
  103.     button.addActionListener(new ActionListener() {
  104.         public void actionPerformed(ActionEvent e) {
  105.         splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  106.         }
  107.     });
  108.     group.add(button);
  109.     buttonWrapper.add(button);
  110.  
  111.     /* Create a check box as to whether or not the split pane continually
  112.        lays out the component when dragging. */
  113.     JCheckBox checkBox = new JCheckBox("Continuous Layout");
  114.     checkBox.setMnemonic('C');
  115.     checkBox.setSelected(true);
  116.  
  117.     checkBox.addChangeListener(new ChangeListener() {
  118.         public void stateChanged(ChangeEvent e) {
  119.         splitPane.setContinuousLayout(((JCheckBox)e.getSource()).isSelected());
  120.         }
  121.     });
  122.     buttonWrapper.add(checkBox);
  123.     wrapper.add(buttonWrapper);
  124.  
  125.     /* Create a text field to change the divider size. */
  126.     Box                      tfWrapper;
  127.     JTextField               tf;
  128.     JLabel                   label;
  129.  
  130.     tf = new JTextField();
  131.         tf.setText(new Integer(splitPane.getDividerSize()).toString());
  132.     tf.setColumns(5);
  133.     tf.getAccessibleContext().setAccessibleName("Divider Size");
  134.     tf.addActionListener(new ActionListener() {
  135.         public void actionPerformed(ActionEvent e) {
  136.         String           value = ((JTextField)e.getSource()).getText();
  137.         int              newSize;
  138.  
  139.         try {
  140.             newSize = Integer.parseInt(value);
  141.         } catch (Exception ex) {
  142.             newSize = -1;
  143.         }
  144.         if(newSize > 0)
  145.             splitPane.setDividerSize(newSize);
  146.         else
  147.             JOptionPane.showMessageDialog(splitPane, "Invalid Divider Size", "Error", JOptionPane.ERROR_MESSAGE);
  148.         }
  149.     });
  150.     label = new JLabel("Divider Size");
  151.     tfWrapper = new Box(BoxLayout.X_AXIS);
  152.     tfWrapper.add(label);
  153.     tfWrapper.add(Box.createHorizontalStrut(10));
  154.     tfWrapper.add(tf);
  155.         label.setLabelFor(tf);
  156.     label.setDisplayedMnemonic('z');
  157.     tfWrapper.add(Box.createHorizontalStrut(Short.MAX_VALUE));
  158.     wrapper.add(tfWrapper);
  159.  
  160.     /* Create a text field that will change the preferred/minimum size
  161.        of the left component. */
  162.     tf = new JTextField(String.valueOf(leftGrid.getPreferredSize().width));
  163.     tf.setColumns(5);
  164.     tf.getAccessibleContext().setAccessibleName("First Component minimum size");
  165.     tf.addActionListener(new ActionListener() {
  166.         public void actionPerformed(ActionEvent e) {
  167.         String           value = ((JTextField)e.getSource()).getText();
  168.         int              newSize;
  169.  
  170.         try {
  171.             newSize = Integer.parseInt(value);
  172.         } catch (Exception ex) {
  173.             newSize = -1;
  174.         }
  175.         if(newSize > 10)
  176.             leftGrid.setPreferredSize(newSize);
  177.         else
  178.             JOptionPane.showMessageDialog(splitPane, "Invalid Minimum Size, must be greater than 10", "Error", JOptionPane.ERROR_MESSAGE);
  179.         }
  180.     });
  181.     label = new JLabel("First Components Minimum Size");
  182.     tfWrapper = new Box(BoxLayout.X_AXIS);
  183.     tfWrapper.add(label);
  184.     tfWrapper.add(Box.createHorizontalStrut(10));
  185.     tfWrapper.add(tf);
  186.     tfWrapper.add(Box.createHorizontalStrut(Short.MAX_VALUE));
  187.         label.setLabelFor(tf);
  188.     label.setDisplayedMnemonic('i');
  189.     wrapper.add(tfWrapper);
  190.     
  191.     /* Create a text field that will change the preferred/minimum size
  192.        of the right component. */
  193.     tf = new JTextField(String.valueOf(rightGrid.getPreferredSize().
  194.                        width));
  195.     tf.setColumns(5);
  196.     tf.getAccessibleContext().setAccessibleName("Second Component minimum size");
  197.     tf.addActionListener(new ActionListener() {
  198.         public void actionPerformed(ActionEvent e) {
  199.         String           value = ((JTextField)e.getSource()).getText();
  200.         int              newSize;
  201.  
  202.         try {
  203.             newSize = Integer.parseInt(value);
  204.         } catch (Exception ex) {
  205.             newSize = -1;
  206.         }
  207.         if(newSize > 10)
  208.             rightGrid.setPreferredSize(newSize);
  209.         else
  210.             JOptionPane.showMessageDialog(splitPane, "Invalid Minimum Size, must be greater than 10", "Error", JOptionPane.ERROR_MESSAGE);
  211.         }
  212.     });
  213.     label = new JLabel("Second Components Minimum Size");
  214.     tfWrapper = new Box(BoxLayout.X_AXIS);
  215.     tfWrapper.add(label);
  216.     tfWrapper.add(Box.createHorizontalStrut(10));
  217.     tfWrapper.add(tf);
  218.     tfWrapper.add(Box.createHorizontalStrut(Short.MAX_VALUE));
  219.         label.setLabelFor(tf);
  220.     label.setDisplayedMnemonic('n');
  221.     wrapper.add(tfWrapper);
  222.  
  223.     add(wrapper, BorderLayout.SOUTH);
  224.     }
  225. }
  226.  
  227. class GridComponent extends Component
  228. {
  229.     /** Number of grids to show. */
  230.     protected int            gridCount;
  231.     /** Size of each grid. */
  232.     protected int            gridSize;
  233.     /** Color for lines. */
  234.     protected Color          currentColor;
  235.     /** Preferred size. */
  236.     protected int            preferredSize;
  237.  
  238.     public GridComponent(int gridCount) {
  239.     this.gridCount = gridCount;
  240.     gridSize = 10;
  241.     currentColor = Color.lightGray;
  242.     }
  243.  
  244.     /**
  245.      * Sets the preferred size to value.
  246.      */
  247.     public void setPreferredSize(int value) {
  248.     preferredSize = value;
  249.     }
  250.  
  251.     /**
  252.      * Returns the preferred size, which is the value of setPreferredSize
  253.      * as a Dimension.
  254.      */
  255.     public Dimension getPreferredSize() {
  256.     return new Dimension(preferredSize, preferredSize);
  257.     }
  258.  
  259.     /**
  260.      * Returns the minimum size. JSplitPane keys of the minimum size to
  261.      * determine where the divider can drag to. If the minimum size of the
  262.      * two components being split are greater than the current size of the
  263.      * splitpane, the splitpane will not allow you to drag the divider 
  264.      * around.
  265.      */
  266.     public Dimension getMinimumSize() {
  267.     return getPreferredSize();
  268.     }
  269.  
  270.     /**
  271.      * Resets the <code>currentColor</code> and messages super.
  272.      */
  273.     public void setBounds(int x, int y, int width, int height) {
  274.     int          minSize = Math.min(width, height);
  275.  
  276.     if(minSize < 100)
  277.         currentColor = Color.red;
  278.     else if(minSize < 200)
  279.         currentColor = Color.blue;
  280.     else if(minSize < 300)
  281.         currentColor = Color.yellow;
  282.     else
  283.         currentColor = Color.white;
  284.  
  285.     gridSize = Math.max(1, minSize / gridCount);
  286.  
  287.     super.setBounds(x, y, width, height);
  288.     }
  289.  
  290.     /**
  291.      * Paints the grid.
  292.      */
  293.     public void paint(Graphics g) {
  294.     Rectangle        paintBounds = g.getClipBounds();
  295.  
  296.     // g.setColor(Color.lightGray);
  297.     // g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width,
  298.     //        paintBounds.height);
  299.     if(gridSize > 0) {
  300.         g.setColor(currentColor);
  301.  
  302.         int              maxY = paintBounds.y + paintBounds.height;
  303.         int              maxX = paintBounds.x + paintBounds.width;
  304.         int              drawMinY = paintBounds.y / gridSize * gridSize;
  305.         int              drawMaxY = maxY / gridSize * gridSize;
  306.         int              drawMinX = paintBounds.x / gridSize * gridSize;
  307.         int              drawMaxX = maxX / gridSize * gridSize;
  308.         int              counter;
  309.  
  310.         for(counter = drawMinX; counter <= drawMaxX; counter += gridSize)
  311.         g.drawLine(counter, paintBounds.y, counter, maxY);
  312.         for(counter = drawMinY; counter <= drawMaxY; counter += gridSize)
  313.         g.drawLine(paintBounds.x, counter, maxX, counter);
  314.     }
  315.     }
  316. }
  317.